#pragma once
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
using namespace std;
class Util
{
public:
    static string GetHeaderValue(const string &request, const string &header_name)
    {
        size_t start_pos = request.find(header_name + ": ");
        if (start_pos == string::npos)
            return "";

        start_pos += header_name.length() + 2; // 跳过": "
        size_t end_pos = request.find("\r\n", start_pos);
        if (end_pos == string::npos)
            return "";

        return request.substr(start_pos, end_pos - start_pos);
    }
    static int FileSize(const string &filename)
    {
        ifstream in(filename, std::ios::binary);
        if (!in.is_open())
            return -1;
        in.seekg(0, in.end);
        int filesize = in.tellg();
        in.seekg(0, in.beg);
        in.close();
        return filesize;
    }
    static bool ReadFileContent(const string &filename, string *out)
    {
        int filesize = FileSize(filename);
        if (filesize > 0)
        {
            ifstream in(filename);
            if (!in.is_open())
                return false;
            out->resize(filesize);
            in.read((char *)(out->c_str()), filesize);
            in.close();
        }
        else
        {
            return false;
        }

        return true;
    }
    static bool GetOneLine(string &req_str, string *out, string separator)
    {
        auto pos = req_str.find(separator);
        if (pos == string::npos)
        {
            return false;
        }
        *out = req_str.substr(0, pos);
        req_str.erase(0, pos + separator.size());
        return true;
    }
};